package com.cloudxingkong.dianyinke.Student.Adapter;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cloudxingkong.dianyinke.NewTeacher.Utils.WordWrapView;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Student.bean.MyAppointmentEntity;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by ningfei on 2016/3/22.
 * 约课adapter
 */
public class SelectCurriculaAdapter extends BaseAdapter {
	private List<MyAppointmentEntity.ListBean> list;
	private Context context;
	private onTouchPosition onTouchPosition;

	public SelectCurriculaAdapter (List<MyAppointmentEntity.ListBean> list, Context context, onTouchPosition onTouchPosition) {
		this.list = list;
		this.context = context;
		this.onTouchPosition = onTouchPosition;

	}

	@Override
	public int getCount () {
		return list == null ? 0 : list.size ();
	}

	@Override
	public Object getItem (int position) {
		return list == null ? null : list.get (position);
	}

	@Override
	public long getItemId (int position) {
		return position;
	}

	@Override
	public View getView (final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			convertView = View.inflate (context, R.layout.select_curricula_item, null);
			holder = new ViewHolder ();
			holder.YK_tv_laoshi = (TextView) convertView.findViewById (R.id.YK_tv_laoshi);//老师
			holder.YK_tv_kecheng = (TextView) convertView.findViewById (R.id.YK_tv_kecheng);//课程
			holder.YK_tv_neirong = (TextView) convertView.findViewById (R.id.YK_tv_neirong);//内容
			holder.YK_tv_jiage = (TextView) convertView.findViewById (R.id.sc_tv_jiage);//价格
			holder.yk_icon = (CircleImageView) convertView.findViewById (R.id.yk_icon);
			holder.ll_distance = (LinearLayout) convertView.findViewById (R.id.ll_yk);
			holder.wwv = ((WordWrapView) convertView.findViewById (R.id.yk_view));
			convertView.setTag (holder);
		} else {
			holder = (ViewHolder) convertView.getTag ();
		}
		onTouchPosition.onTouch (position);
		holder.YK_tv_laoshi.setText (list.get (position).getMemname () + "");
		holder.YK_tv_kecheng.setText (list.get (position).getTitle () + "");
		holder.YK_tv_neirong.setText (list.get (position).getNote () + "");
		holder.YK_tv_jiage.setText ("￥" + list.get (position).getPrice () + "");

		ImageLoader.getInstance ().displayImage (list.get (position).getAvatar (), holder.yk_icon);

		String label = list.get (position).getLabel () + "";//标签
		if (label != null) {
			String[] split = label.split (",");
			for (int i = 0; i < split.length; i++) {
				TextView tv = new TextView (context);
				tv.setGravity (Gravity.CENTER);
				tv.setTextColor (context.getResources ().getColor (R.color.white));
				tv.setBackgroundResource (R.drawable.shape_uw_label_main);
				tv.setText (split[i]);
				if (holder.wwv.getChildCount () < split.length) {
					holder.wwv.addView (tv);
				}
				//Log.e("test", position + " -- " + holder.wwv.getChildCount() + " -- " + split.length + " -- " + split[i] + " --- " + tv.getText());
			}
		} else {
			Log.e ("test", "标签为空");
		}


		return convertView;
	}

	private class ViewHolder {
		TextView YK_tv_laoshi, YK_tv_kecheng, YK_tv_neirong, YK_tv_jiage;
		LinearLayout ll_distance;
		WordWrapView wwv;
		CircleImageView yk_icon;
	}

	//接口回调position
	public interface onTouchPosition {
		public void onTouch (int position);
	}
}
